package com.hr.borrowing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.hr.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hr.common.annotation.Log;
import com.hr.common.core.controller.BaseController;
import com.hr.common.core.domain.AjaxResult;
import com.hr.common.enums.BusinessType;
import com.hr.borrowing.domain.Borrowing;
import com.hr.borrowing.service.IBorrowingService;
import com.hr.common.utils.poi.ExcelUtil;
import com.hr.common.core.page.TableDataInfo;

/**
 * 借支权限Controller
 * 
 * @author guan
 * @date 2024-07-17
 */
@RestController
@RequestMapping("/manage/borrowing")
public class BorrowingController extends BaseController
{
    @Autowired
    private IBorrowingService borrowingService;

    /**
     * 查询借支权限列表
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowing:list')")
    @GetMapping("/list")
    public TableDataInfo list(Borrowing borrowing)
    {
        startPage();
        List<Borrowing> list = borrowingService.selectBorrowingList(borrowing);
        return getDataTable(list);
    }

    /**
     * 获取借支权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowing:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(borrowingService.selectBorrowingByUserId(userId));
    }

    /**
     * 非管理员查询自身借支权限
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowing:info')")
    @GetMapping(value = "/info")
    public AjaxResult getInfoByUserId()
    {
        LoginUser loginUser = getLoginUser();
        Borrowing data = borrowingService.selectBorrowingById(loginUser.getUserId());
        if (data == null){
            return error("该用户没有借支权限");
        }
        return success(data);
    }

    /**
     * 新增借支权限
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowing:add')")
    @Log(title = "借支权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Borrowing borrowing)
    {
        return toAjax(borrowingService.insertBorrowing(borrowing));
    }

    /**
     * 修改借支权限
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowing:edit')")
    @Log(title = "借支权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Borrowing borrowing)
    {
        return toAjax(borrowingService.updateBorrowing(borrowing));
    }

    /**
     * 删除借支权限
     */
    @PreAuthorize("@ss.hasPermi('manage:borrowing:remove')")
    @Log(title = "借支权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(borrowingService.deleteBorrowingByUserIds(userIds));
    }


}
